package morenoapp.util;

import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.swing.ActionMap;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import morenoapp.model.app.MorenoApplicationData;
import org.apache.commons.io.FileUtils;
import org.apache.poi.poifs.property.Child;

/**
 *
 * @author dmitry.mamonov
 */
public class MorenoSwing {
    public static void setupNumbus() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            throw new ShouldNotHappenException(ex);
        } catch (ClassNotFoundException ex) {
            throw new ShouldNotHappenException(ex);
        } catch (InstantiationException ex) {
            throw new ShouldNotHappenException(ex);
        } catch (IllegalAccessException ex) {
            throw new ShouldNotHappenException(ex);
        }
    }

    public static File constructResoirceFileName(Class<?> _type) {
        return new File("src", _type.getPackage().getName().replace(".", "/") + "/resources/" + _type.getSimpleName() + ".properties");
    }

    public static void showComponentInFrame(JComponent c) {
        JFrame frame = new JFrame("" + c.getName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(c);
        frame.pack();
        frame.setVisible(true);
    }

    public static void generateActionsProperties(Class<?> _type, ActionMap _actions) throws IOException {
        Properties props = new Properties();
        File resourceFile = constructResoirceFileName(_type);
        List<String> resorcesLines = FileUtils.readLines(resourceFile);
        props.load(new FileInputStream(resourceFile));
        for (String key : (List<String>) (List) Arrays.asList((Object[]) _actions.keys())) {
            if (props.containsKey(key + ".Action.text") == false) {
                resorcesLines.add("######################################################");
                resorcesLines.add(key + ".Action.text = " + key);
                resorcesLines.add(key + ".Action.icon = action_.png");
                resorcesLines.add(key + ".Action.accelerator = ");
                resorcesLines.add(key + ".Action.shortDescription = ");
                resorcesLines.add("");

            }
        }
        FileUtils.writeLines(resourceFile, resorcesLines);
    }

    public static JPanel line(Component... components){
        JPanel panel = new JPanel();
        BoxLayout box = new BoxLayout(panel, BoxLayout.LINE_AXIS);
        panel.setLayout(box);

        for(Component comp:components){
            panel.add(comp);
        }
        return panel;
    }
}
